﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using Utility;
using System.Data;
using Hxj.Data;
using Utility.Model;

namespace MsgCenter.Web.Pages.User
{
    public partial class Option : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                txtDeptName.ToReadOnly();
                BindRoles();
                if (strType == "Add")
                {
                    this.linkSave.Visible = true;
                }
                else if (strType == "Edit")
                {
                    this.linkSave.Visible = true;
                    this.txtUserName.ToReadOnly();
                    this.GetModel();
                }
                else if (strType == "Look")
                {
                    this.txtUserName.ToReadOnly();
                    this.GetModel();
                }
            }
        }

        private void GetModel()
        {
            Sys_User model = BLL.BLLServiceHelper.GetUserService().GetModel(strGuID);
            if (model != null)
            {
                if (model.IsAdmin != null)
                {
                    chkIsAdmin.Checked = model.IsAdmin.Value;
                }
                txtName.Value = model.Name;
                txtUserName.Value = model.UserName;
                txtPassword.Value = model.Password.ToSafeDecrypt();
                txtEmail.Value = model.Email;
                txtMobile.Value = model.Mobile;
                txtRtxLoginName.Value = model.RtxLoginName;
                txtWebChat.Value = model.WebChat;
                txtCode.Value = model.Code;
                if (model.BirthDay != null)
                {
                    txtBirthDay.Value = model.BirthDay.DateTimeToyyyy_MM_dd();
                }
                if (model.DeptGuID != null)
                {
                    Sys_Dept modelDept = BLL.BLLServiceHelper.GetDeptService().GetModel(model.DeptGuID.ToSafeString());
                    if (modelDept != null)
                    {
                        txtDeptName.Value = modelDept.DeptName;
                        hidDeptID.Value = modelDept.ID.ToSafeString();
                    }
                }
            }
            using (DataTable dtUserRoles = BLL.BLLServiceHelper.GetUserService().GetUserRoles(strGuID))
            {
                foreach (DataRow dr in dtUserRoles.Rows)
                {
                    if (listRole.Items.FindByValue(dr["RoleID"].ToSafeString()) != null)
                    {
                        listRole.Items.FindByValue(dr["RoleID"].ToSafeString()).Selected = true;
                    }
                }
            }
        }

        private void BindRoles()
        {
            using (DataTable dtRole = BLL.BLLServiceHelper.GetRoleService().GetRoleTable(new WhereClip().And(Sys_Role._.IsDelete == false)))
            {
                foreach (DataRow dr in dtRole.Rows)
                {
                    listRole.Items.Add(new ListItem(dr["RoleName"].ToSafeString(), dr["RoleID"].ToSafeString()));
                }
            }
        }

        private List<Sys_UserRoles> GetUserRoles(string strUserID)
        {
            List<Sys_UserRoles> list = new List<Sys_UserRoles>();
            for (int i = 0; i < listRole.Items.Count; i++)
            {
                if (listRole.Items[i].Selected == true)
                {
                    Sys_UserRoles model = new Sys_UserRoles();
                    model.UserID = strUserID;
                    model.RoleID = listRole.Items[i].Value;
                    list.Add(model);
                }
            }
            return list;
        }

        protected void linkSave_Click(object sender, EventArgs e)
        {
            try
            {
                Sys_User model = null;
                if (strType == "Add")
                {
                    model = BLL.BLLServiceHelper.GetBaseDataService().GetModel<Sys_User>(new WhereClip().And(Sys_User._.IsDelete == false && Sys_User._.UserName == txtUserName.Value));
                    if (model != null)
                    {
                        base.ShowAlert("保存数据出错:已经存在相同了用户名");
                        return;
                    }
                }
                if (strGuID != "")
                {
                    model = BLL.BLLServiceHelper.GetUserService().GetModel(strGuID);
                }
                else
                {
                    model = new Sys_User();
                }
                model.IsAdmin = chkIsAdmin.Checked;
                model.Name = txtName.Value;
                model.UserName = txtUserName.Value.ToLower();
                model.DeptGuID = hidDeptID.Value;
                model.Password = txtPassword.Value.ToSafeEncrypt();
                model.CreateUser = base.Account;
                model.Email = txtEmail.Value;
                model.Mobile = txtMobile.Value;
                model.RtxLoginName = txtRtxLoginName.Value;
                model.WebChat = txtWebChat.Value;
                model.Code = txtCode.Value;
                if (!txtBirthDay.Value.IsNullOrEmpty())
                {
                    model.BirthDay = txtBirthDay.Value.ToSafeDateTime();
                }
                BLL.BLLServiceHelper.GetUserService().Save(model, GetUserRoles(model.UserID));
                base.ShowAlertScript("Close_Reload();");
            }
            catch (Exception ex)
            {
                base.ShowAlert("保存数据出错:" + ex.Message);
            }
        }
    }
}